package com.aaa.service.impl.mht;

import com.aaa.entity.MyPage;
import com.aaa.entity.mht.MhtQuestion;
import com.aaa.mapper.mht.MhtMapper;
import com.aaa.service.mht.MhtService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MhtServiceImpl implements MhtService {

    @Autowired
    private MhtMapper mhtMapper;

    /**
     * 导入excel数据
     * @param list
     * @return
     */
    @Override
    public Boolean inductsExcelFile(List<Object> list) {
        int i=-1;
        for (Object excelList: list) {
            MhtQuestion excelList1 = (MhtQuestion) excelList;
            excelList1.setCreatetime(new Date());
            int insert = mhtMapper.insert(excelList1);
            i+=insert;
        }
        return i>=0;
    }

    /**
     * 查询全部的mht题目业务 并分页
     * @return
     * @param myPage
     */
    @Override
    public PageInfo findQuestions(MyPage myPage,MhtQuestion mhtQuestion) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<MhtQuestion> allQuestion = mhtMapper.findAllQuestion(mhtQuestion);
        List<MhtQuestion> questions = mhtMapper.selectAll();
        PageInfo pageInfo = new PageInfo(allQuestion);
        return pageInfo;
    }

    /**
     * 检测题目编号是否存在
     * @param titleid
     * @return
     */
    @Override
    public Boolean checkTitleId(Integer titleid) {
        MhtQuestion mhtQuestion = new MhtQuestion();
        mhtQuestion.setTitleid(titleid);
        MhtQuestion mhtQuestion1 = mhtMapper.selectOne(mhtQuestion);
        if(mhtQuestion1!=null){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 添加测试题业务
     * @param mhtQuestion
     * @return
     */
    @Override
    public Boolean addQuestion(MhtQuestion mhtQuestion) {
        int i=-1;
        Integer id = mhtQuestion.getId();
        if(id==null){
            mhtQuestion.setProper("是");
            mhtQuestion.setIncorrect("否");
            mhtQuestion.setCreatetime(new Date());
            i = mhtMapper.insertSelective(mhtQuestion);
        }else{
            mhtQuestion.setUpdatetime(new Date());
            i = mhtMapper.updateByPrimaryKeySelective(mhtQuestion);
        }
        return i>=0;
    }

    /**
     * 删除mht测试题业务
     * @param mhtQuestion
     * @return
     */
    @Override
    public Boolean delQuestion(MhtQuestion mhtQuestion) {
        int i = mhtMapper.deleteByPrimaryKey(mhtQuestion);
        return i>=0;
    }

    /**
     * 查询全部的题目（做题页面使用）
     * @param myPage
     * @return
     */
    @Override
    public PageInfo findAllTile(MyPage myPage) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<MhtQuestion> questions = mhtMapper.selectAll();
        PageInfo pageInfo = new PageInfo(questions);
        return pageInfo;
    }
}
